package vuelta.general.rss;

import java.util.List;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import vuelta.general.R;
import vuelta.general.util.web.WebViewGiro;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	private Context context;

	private List<JSONObject> jsonObject;

	private boolean mFromEnglish;

	public ExpandableListAdapter(Context context) {
		this.context = context;
	}

	public ExpandableListAdapter(Context context, boolean fromEnglish) {
		this.context = context;
		mFromEnglish = fromEnglish;
	}

	public void setList(List<JSONObject> jsonObject) {
		this.jsonObject = jsonObject;
		notifyDataSetChanged();
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public Object getChild(int groupPosition, int childPosition) {
		try {
			return jsonObject.get(groupPosition).getString("description");
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		String description = (String) getChild(groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.child_layout, null);
		}

		Button browser = (Button) convertView.findViewById(R.id.browser_button);
		browser.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				try {
					Intent intent = new Intent(context, WebViewGiro.class);
					if (mFromEnglish) {
						if (Locale.getDefault().equals(Locale.FRANCE) || Locale.getDefault().equals(Locale.FRENCH)) {
							String url = "http://sports.feedsportal.com"
									+ jsonObject.get(groupPosition).getString("url");
							intent.putExtra("url", url);
						} else {
							String url = "http://feedproxy.google.com"
									+ jsonObject.get(groupPosition).getString("url");
							intent.putExtra("url", url);
						}
					} else {
						intent.putExtra("url", jsonObject.get(groupPosition).getString("url"));
					}
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(intent);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});

		TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
		tv.setText(description);

		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	public Object getGroup(int groupPosition) {
		return jsonObject.get(groupPosition);
	}

	public int getGroupCount() {
		if (jsonObject != null) {
			return jsonObject.size();
		} else
			return 0;
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		JSONObject group = (JSONObject) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.group_layout, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
		try {
			tv.setText(group.getString("title"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return convertView;
	}

}